//
//  ZSSecurityUtil.m
//  CheFu365
//
//  Created by safiri on 2017/8/31.
//  Copyright © 2017年 safiri. All rights reserved.
//

#import "ZSSecurityUtil.h"
#import "GTMBase64.h"
#import "NSData+ZSEncryption.h"

@implementation ZSSecurityUtil

#pragma mark - Encrypt and Decrypt
// AES加解密
+ (NSString *)encryptAES128ForString:(NSString *)string key:(NSString *)key giv:(NSString *)giv {
    //将NSString转化为NSData
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    //使用密码对NSData进行加密
    NSData *encryptedData = [data AES128EncryptWithKey:key gIv:giv];
    //返回进行base64进行转码的加密字符串
    return [GTMBase64 stringByEncodingData:encryptedData];
}
+ (NSString *)decryptAES128ForString:(NSString *)string key:(NSString *)key giv:(NSString *)giv {
    //base64解密
    NSData *decodeBase64Data = [GTMBase64 decodeString:string];
    //使用密码对data进行解密
    NSData *decryData = [decodeBase64Data AES128DecryptWithKey:key gIv:giv];
    //将解了密码的NSData转化为NSString
    return [[NSString alloc] initWithData:decryData encoding:NSUTF8StringEncoding];
}
//DES加解密
+ (NSString *)encryptDESForString:(NSString *)string key:(NSString *)key gIv:(NSString *)iv{
    //将NSString转化为NSData
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    //使用密码对NSData进行加密
    NSData *encryptedData = [data DESEncryptWithKey:key gIv:iv];
    //返回进行base64进行转码的加密字符串
    return [GTMBase64 stringByEncodingData:encryptedData];
}
+ (NSString *)decryptDESForString:(NSString *)string key:(NSString *)key gIv:(NSString *)iv{
    //base64解密
    NSData *decodeBase64Data = [GTMBase64 decodeString:string];
    //使用密码对data进行解密
    NSData *decryData = [decodeBase64Data DESDecryptWithKey:key gIv:iv];
    //将解了密码的NSData转化为NSString
    return [[NSString alloc] initWithData:decryData encoding:NSUTF8StringEncoding];
}
// 3DES
+ (NSString *)encrypt3DESForString:(NSString *)string key:(NSString *)key gIv:(NSString *)iv {
    //将NSString转化为NSData
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    //使用密码对NSData进行加密
    NSData *encryptedData = [data DES3EncryptWithKey:key gIv:iv];
    //返回进行base64进行转码的加密字符串
    return [GTMBase64 stringByEncodingData:encryptedData];
}

+ (NSString *)decrypt3DESForString:(NSString *)string key:(NSString*)key gIv:(NSString *)iv {
    //base64解密
    NSData *decodeBase64Data = [GTMBase64 decodeString:string];
    //使用密码对data进行解密
    NSData *decryData = [decodeBase64Data DES3DecryptWithKey:key gIv:iv];
    //将解了密码的NSData转化为NSString
    return [[NSString alloc] initWithData:decryData encoding:NSUTF8StringEncoding];
}

#pragma mark - hash
+ (NSString *)md2ForString:(NSString *)string {
    return [[string dataUsingEncoding:NSUTF8StringEncoding] md2String];
}
+ (NSString *)md4ForString:(NSString *)string {
    return [[string dataUsingEncoding:NSUTF8StringEncoding] md4String];
}
+ (NSString *)md5ForString:(NSString *)string {
    return [[string dataUsingEncoding:NSUTF8StringEncoding] md5String];
}

+ (NSString *)sha1ForString:(NSString *)string {
    return [[string dataUsingEncoding:NSUTF8StringEncoding] sha1String];
}

+ (NSString *)sha224ForString:(NSString *)string {
    return [[string dataUsingEncoding:NSUTF8StringEncoding] sha224String];
}

+ (NSString *)sha256ForString:(NSString *)string {
    return [[string dataUsingEncoding:NSUTF8StringEncoding] sha256String];
}
+ (NSString *)sha384ForString:(NSString *)string {
    return [[string dataUsingEncoding:NSUTF8StringEncoding] sha384String];
}
+ (NSString *)sha512ForString:(NSString *)string {
    return [[string dataUsingEncoding:NSUTF8StringEncoding] sha512String];
}

+ (NSString *)hmacMD5ForString:(NSString *)string withKey:(NSString *)key {
    return [[string dataUsingEncoding:NSUTF8StringEncoding]
            hmacMD5StringWithKey:key];
}
+ (NSString *)hmacSHA1ForString:(NSString *)string withKey:(NSString *)key {
    return [[string dataUsingEncoding:NSUTF8StringEncoding]
            hmacSHA1StringWithKey:key];
}

+ (NSString *)hmacSHA224ForString:(NSString *)string withKey:(NSString *)key {
    return [[string dataUsingEncoding:NSUTF8StringEncoding]
            hmacSHA224StringWithKey:key];
}

+ (NSString *)hmacSHA256ForString:(NSString *)string withKey:(NSString *)key {
    return [[string dataUsingEncoding:NSUTF8StringEncoding]
            hmacSHA256StringWithKey:key];
}

+ (NSString *)hmacSHA384ForString:(NSString *)string withKey:(NSString *)key {
    return [[string dataUsingEncoding:NSUTF8StringEncoding]
            hmacSHA384StringWithKey:key];
}

+ (NSString *)hmacSHA512ForString:(NSString *)string withKey:(NSString *)key {
    return [[string dataUsingEncoding:NSUTF8StringEncoding]
            hmacSHA512StringWithKey:key];
}
+ (NSString *)crc32ForString:(NSString *)string {
    return [[string dataUsingEncoding:NSUTF8StringEncoding] crc32String];
}
#pragma mark - Encode and decode

+ (NSString *)encodeBase64String:(NSString *)normalString {
    NSData *data = [GTMBase64 encodeData:[normalString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]];
    NSString *base64String = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    return base64String;
}

+ (NSString *)decodeBase64String:(NSString *)base64String {
    NSData *data = [GTMBase64 decodeData:[base64String dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]];
    NSString *decodeString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    return decodeString;
}

//将NSData转成16进制
+ (NSString *)convertDataToHexStr:(NSData *)data {
    if (!data || [data length] == 0) {
        return @"";
    }
    NSMutableString *string = [[NSMutableString alloc] initWithCapacity:[data length]];
    
    [data enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange, BOOL *stop) {
        unsigned char *dataBytes = (unsigned char*)bytes;
        for (NSInteger i = 0; i < byteRange.length; i++) {
            NSString *hexStr = [NSString stringWithFormat:@"%x", (dataBytes[i]) & 0xff];
            if ([hexStr length] == 2) {
                [string appendString:hexStr];
            } else {
                [string appendFormat:@"0%@", hexStr];
            }
        }
    }];
    return string;
}

//将16进制字符串转成NSData
+ (NSData *)convertHexStrToData:(NSString *)str {
    if (!str || [str length] == 0) {
        return nil;
    }
    
    NSMutableData *hexData = [[NSMutableData alloc] initWithCapacity:8];
    NSRange range;
    if ([str length] % 2 == 0) {
        range = NSMakeRange(0, 2);
    } else {
        range = NSMakeRange(0, 1);
    }
    for (NSInteger i = range.location; i < [str length]; i += 2) {
        unsigned int anInt;
        NSString *hexCharStr = [str substringWithRange:range];
        NSScanner *scanner = [[NSScanner alloc] initWithString:hexCharStr];
        
        [scanner scanHexInt:&anInt];
        NSData *entity = [[NSData alloc] initWithBytes:&anInt length:1];
        [hexData appendData:entity];
        
        range.location += range.length;
        range.length = 2;
    }
    return hexData;
}

@end
